/* 编辑页面清空错误信息提示 */
function clearErrorMsg() { $("#errorMsg").empty();}

/* 点击【新增/编辑】后加载编辑页面 */
function loadEditData(url) { $("#main-page").load(url, {"page":$(".active span").text() - 1}); }

/* 点击【编辑完成】后的操作 */
function loadEditCompleteDate() {
    let form = $("#form-data");
    let url = form.attr("action");
    let editType = $("#editType").val();
    if (editType === 'ADD') url = url.substring(0, url.lastIndexOf("/") + 1) + "add";
    if (editType === 'ADDOUTS') url = url.substring(0, url.lastIndexOf("/") + 1) + "add";
    if (editType === 'EDIT') url = url.substring(0, url.lastIndexOf("/") + 1) + "doEdit";

    $.post(url, form.serialize(), function (data) {
        data = eval('(' + data + ')');
        if (data.success) {
            let page = $("#page").val();
            let newUrl = url.substring(0, url.lastIndexOf("/") + 1) + "list";
            $("#main-page").load(newUrl, {"page":page === undefined ? 0 : page});
        } else { $("#errorMsg").html(data.message); }

    })
}

/* 点击【删除】后当前页面加载数据列表 */
function loadDeleteData(url) {$("#main-page").load(url, {"page":$(".active span").text() - 1});}

/* 点击【查询】后当前页面加载数据列表 */
function loadSearchData() {
    let form = $("#form-data");
    $("#main-page").load(form.attr("action"), form.serialize());
}

/* 点击【返回】后加载数据列表 */
function loadListData(url) {
    let page = $("#page").val();
    $("#main-page").load(url, {"page":page === undefined ? 0 : page});}

const Misc = function () {
};

Misc.loadSelect = function(parentId, url) {
    let $els = [];

    if ($.trim(parentId).length === 0) { $els = $("select.code-select"); }
    else { $els = $("#" + parentId + " select.code-select"); }

    if(!$($els)[0]) { return; }

    for (let i = 0; i < $els.length; ++i) {
        let $el = $els.eq(i);
        let id = $el.attr("id");
        if ($.trim(id).length === 0) {
            id = ("code-select-" + i + new Date().getTime() + Math.random()).replace(".", "");
            $el.attr("id", id)
        }
        let params = {
            dataId: id,
            clazz: $.trim($el.attr("data-clazz"))
        };
        if ("" === params.clazz) {
            continue;
        }

        $.post(url, params, function(data) {
            let html = "";
            let dataId = "";
            if ("undefined" !== typeof(data)) {
                data = eval("(" + data + ")");
                if (data["success"]) {
                    dataId = data["message"];
                    let $dataEl = $("#" + dataId);
                    let empty = $.trim($dataEl.attr("data-empty"));
                    let value = $.trim($dataEl.attr("data-value"));
                    if ($.trim(empty).length !== 0) {
                        if (value === "") {
                            html += "<option value=\"\" selected=\"selected\">" + empty + "</option>";
                        } else {
                            html += "<option value=\"\">" + empty + "</option>";
                        }
                    }

                    data = eval("(" + data["data"] + ")");
                    for (let j = 0; j < data.length; ++j) {
                        if ($.trim(value) === data[j]["code"]) {
                            html += "<option selected=\"selected\" value=\"" + data[j]["code"] + "\">" + data[j]["name"] + "</option>";
                        } else {
                            html += "<option value=\"" + data[j]["code"] + "\">" + data[j]["name"] + "</option>";
                        }
                    }
                    $dataEl.html(html);
                }
            }
        });
    }
};
